package me.flyray.bsin.server.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.constants.ResponseCode;
import me.flyray.bsin.context.BsinServiceContext;
import me.flyray.bsin.domain.entity.AgentCard;
import me.flyray.bsin.domain.entity.KnowledgeBase;
import me.flyray.bsin.domain.entity.LLMParam;
import me.flyray.bsin.exception.BusinessException;
import me.flyray.bsin.facade.service.LLMService;
import me.flyray.bsin.infrastructure.mapper.LlmMapper;
import me.flyray.bsin.mybatis.utils.Pagination;
import me.flyray.bsin.security.contex.LoginInfoContextHelper;
import me.flyray.bsin.security.domain.LoginUser;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.client.apache.dubbo.annotation.ShenyuDubboService;
import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
import org.apache.shenyu.client.apidocs.annotations.ApiModule;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static me.flyray.bsin.constants.ResponseCode.CONDITION_NOT_EXISTS;
import static me.flyray.bsin.constants.ResponseCode.ID_NOT_ISNULL;

@Slf4j
@ShenyuDubboService(path = "/llm", timeout = 6000)
@ApiModule(value = "llm")
public class LLMServiceImpl implements LLMService {

    @Autowired
    private LlmMapper llmMapper;

    @ApiDoc(desc = "add")
    @ShenyuDubboClient("/add")
    @Override
    public LLMParam add(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        LLMParam llmParam = BsinServiceContext.getReqBodyDto(LLMParam.class, requestMap);
        Optional.ofNullable(llmParam.getTenantId())
                .or(() -> Optional.ofNullable(loginUser.getTenantId()))
                .ifPresentOrElse(
                        llmParam::setTenantId,
                        () -> { throw new BusinessException(ResponseCode.TENANT_ID_NOT_ISNULL); }
                );
        llmMapper.insert(llmParam);
        return llmParam;
    }

    @ApiDoc(desc = "delete")
    @ShenyuDubboClient("/delete")
    @Override
    public void delete(Map<String, Object> requestMap) {
        String serialNo = MapUtils.getString(requestMap, "serialNo");
        if (llmMapper.deleteById(serialNo) == 0){
            throw new BusinessException(ID_NOT_ISNULL);
        }
    }

    @ApiDoc(desc = "edit")
    @ShenyuDubboClient("/edit")
    @Override
    public void edit(Map<String, Object> requestMap) {
        LLMParam llmParam = BsinServiceContext.getReqBodyDto(LLMParam.class, requestMap);
        if (llmMapper.updateById(llmParam) == 0){
            throw new BusinessException(ID_NOT_ISNULL);
        }
    }

    @ApiDoc(desc = "getDetail")
    @ShenyuDubboClient("/getDetail")
    @Override
    public LLMParam getDetail(Map<String, Object> requestMap) {
        String serialNo = MapUtils.getString(requestMap, "serialNo");
        LLMParam llmParam = llmMapper.selectById(serialNo);
        if (llmParam == null) {
            throw new BusinessException(CONDITION_NOT_EXISTS);
        }
        return llmParam;
    }

    @ApiDoc(desc = "getPageList")
    @ShenyuDubboClient("/getPageList")
    @Override
    public IPage<LLMParam> getPageList(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        Object paginationObj =  requestMap.get("pagination");
        Pagination pagination = new Pagination();
        BeanUtil.copyProperties(paginationObj,pagination);

        Page<LLMParam> page = new Page<>(pagination.getPageNum(),pagination.getPageSize());
        LambdaQueryWrapper<LLMParam> warapper = new LambdaQueryWrapper<>();
        warapper.orderByDesc(LLMParam::getCreateTime);
        warapper.eq(LLMParam::getTenantId, loginUser.getTenantId());
        warapper.eq(StringUtils.isNotBlank(loginUser.getMerchantNo()), LLMParam::getMerchantNo, loginUser.getMerchantNo());
        IPage<LLMParam> pageList = llmMapper.selectPage(page,warapper);
        return pageList;
    }

    @ApiDoc(desc = "getList")
    @ShenyuDubboClient("/getList")
    @Override
    public List<LLMParam> getList(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        LLMParam llmParam = BsinServiceContext.getReqBodyDto(LLMParam.class, requestMap);
        String tenantId = (String) requestMap.get("tenantId");
        LambdaQueryWrapper<LLMParam> warapper = new LambdaQueryWrapper<>();
        warapper.eq(LLMParam::getTenantId, tenantId);
        warapper.eq(StringUtils.isNotBlank(loginUser.getMerchantNo()), LLMParam::getMerchantNo, loginUser.getMerchantNo());
        List<LLMParam> llmParamList = llmMapper.selectList(warapper);
        return llmParamList;
    }

    @ApiDoc(desc = "getDefault")
    @ShenyuDubboClient("/getDefault")
    @Override
    public LLMParam getDefault(Map<String, Object> requestMap) {
        return null;
    }

    @ApiDoc(desc = "setDefault")
    @ShenyuDubboClient("/setDefault")
    @Override
    public void setDefault(Map<String, Object> requestMap) {

    }
}
